package com.mall.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.mall.common.Result;
import com.mall.dto.CartDTO;
import com.mall.model.Cart;
import com.mall.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "购物车管理")
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {
    
    private final CartService cartService;
    
    @Operation(summary = "添加商品到购物车")
    @PostMapping
    public Result<Void> addToCart(@Valid @RequestBody CartDTO cartDTO) {
        Cart cart = new Cart();
        cart.setUserId(StpUtil.getLoginIdAsLong());
        cart.setProductId(cartDTO.getProductId());
        cart.setQuantity(cartDTO.getQuantity());
        cartService.addToCart(cart);
        return Result.success();
    }
    
    @Operation(summary = "更新购物车商品数量")
    @PutMapping("/{id}/quantity")
    public Result<Void> updateQuantity(@PathVariable Long id, @RequestParam Integer quantity) {
        cartService.updateQuantity(id, quantity);
        return Result.success();
    }
    
    @Operation(summary = "删除购物车商品")
    @DeleteMapping("/{id}")
    public Result<Void> deleteFromCart(@PathVariable Long id) {
        cartService.deleteFromCart(id);
        return Result.success();
    }
    
    @Operation(summary = "清空购物车")
    @DeleteMapping("/clear")
    public Result<Void> clearCart() {
        cartService.clearCart(StpUtil.getLoginIdAsLong());
        return Result.success();
    }
    
    @Operation(summary = "获取购物车列表")
    @GetMapping("/list")
    public Result<List<Cart>> getCartList() {
        return Result.success(cartService.getUserCart(StpUtil.getLoginIdAsLong()));
    }
} 